package com.macrounion.atv.controller.api;

import com.macrounion.atv.config.FileConfig;
import com.macrounion.atv.processor.RegProcessor;
import com.macrounion.atv.service.dto.req.SettingsReqDto;
import com.macrounion.atv.service.dto.resp.UploadRespDto;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.macrounion.atv.processor.SettingsProcessor;
import com.macrounion.atv.service.entity.Settings;
import com.macrounion.atv.service.entity.User;
import com.macrounion.atv.service.service.SettingsService;
import com.macrounion.base.controller.api.BaseApiController;
import com.macrounion.base.service.dto.StringRespDto;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/settings")
public class SettingsApiController extends BaseApiController<Settings, Settings, SettingsProcessor, SettingsService, Settings, User> {

    @Autowired
    RegProcessor regProcessor;
    @Autowired
    FileConfig customConfig;

    @RequestMapping("/uploadImage")
    public UploadRespDto upload(@RequestParam(value = "file", required = false) MultipartFile file) {
        if (file.isEmpty()) return UploadRespDto.fail("上传失败，请选择文件");
//        String fileName = file.getOriginalFilename();
//        try {
//            String path = FileHandleUtil.upload(file.getInputStream(), "images/", fileName);
//            return UploadRespDto.success(path);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return UploadRespDto.fail("上传失败");
//        }
        String images = this.processor.uploadFile(request,file, FilenameUtils.concat(customConfig.getUploadFileDir(), "images"));
        return  UploadRespDto.success(images);
    }

    @PostMapping("/getSettings")
    public List<Map<String, String>> getValues(@RequestBody List<String> names) {
        return this.processor.getValues(names);
    }

    @PostMapping("/saveSettings")
    public int saveSettings(@RequestBody SettingsReqDto reqDto) {
        return this.processor.saveSettings(reqDto);
    }

    @PostMapping("/updateSettings")
    public int updateSettings(@RequestBody SettingsReqDto reqDto) {
        return this.processor.updateSettings(reqDto);
    }

    @GetMapping("/getRegId")
    public StringRespDto getRegId() {
        return new StringRespDto(regProcessor.getRegId());
    }
}